package game;

import piece.Piece;
import field.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FIR {
    private Board board = new Board();
    private View view = new View(board);

    // true represents black's turn currently, false represents white's turn.
    private boolean state = true;

    // 1 represents Black wins, 2 represents White wins, 0 represents both win.
    private int winner;

    private boolean isOver = false;
    private int currentX; // the current piece's x
    private int currentY; // the current piece's y

    private static final int distance = 39;
    private static final int startX = 26;
    private static final int startY = 26;

    /**
     * To check if the game is over. If so, check who is the winner.
     */
    public void check() {
        if (isOver) {
            if (!state) {
                winner = 2;
                JOptionPane.showMessageDialog(null, "White wins !", "Prompt", JOptionPane.CLOSED_OPTION);
            } else {
                winner = 1;
                JOptionPane.showMessageDialog(null, "Black wins !", "Prompt", JOptionPane.CLOSED_OPTION);
            }
        } else {
            state = !state;
        }
    }

    /**
     * add mouse clicked event
     */
    public void addMouseEvent() {
        board.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!isOver) {
                    super.mouseClicked(e);
                    int x = e.getX();
                    int y = e.getY();
                    if (x >= 15 && x <= 580 && y >= 15 && y <= 580) {
                        x = (int) Math.round((double) (x - startX) / distance);
                        y = (int) Math.round((double) (y - startY) / distance);
                        if (board.canPlace(x, y)) {
                            board.place(x, y, new Piece(state));
                            if ((board.checkX(x, y, state) >= 5)
                                    || (board.checkY(x, y, state) >= 5)
                                    || (board.checkZL(x, y, state) >= 5)
                                    || (board.checkZR(x, y, state) >= 5)) {
                                isOver = true;
                            }
                            check();
                            if (!isOver) {
                                view.setPrompt(state);
                                currentX = x;
                                currentY = y;
                            }
                        }
                    }
                }
            }
        });
    }

    /**
     * add the new button's action
     */
    public void addNewButtonEvent() {
        view.getButton(0).addActionListener(e -> {
            int choice = JOptionPane.showConfirmDialog(null, "Whether to start a new game ?",
                    "Prompt", JOptionPane.YES_NO_OPTION);
            if (choice == 0) {
                board.clearAll();

                /**
                 * if restart the game, the state should be true.
                 */
                state = true;
                isOver = false;
                view.setPrompt(state);
            }
        });
    }

    /**
     * add the undo button's action to retreat one step.
     */
    public void addUndoButtonEvent() {
        view.getButton(1).addActionListener(e -> {
            if (!isOver) {
                board.clear(currentX, currentY);
                state = !state;
                view.setPrompt(state);
            }
        });
    }

    public void addAbountButtonEvent() {
        view.getButton(2).addActionListener(e -> {
            String message= "Five-In-Row\nAuthor: Zerone\nEmail: fgksgf@gmail.com\nV 1.0";
            JOptionPane.showMessageDialog(null,message);
        });
    }
    /**
     * the main part of the game
     */
    public void run() {
        addMouseEvent();
        addNewButtonEvent();
        addUndoButtonEvent();
        addAbountButtonEvent();
        while (true) {
            view.setPromptVisible(!isOver);
            view.display();
        }
    }
}
